'use strict';

var Oriento = require('oriento');
var server = Oriento({ 
	host: 'localhost', 
	port: 2424, 
	username: 'root', 
	password: 'ldj123' });

//连接数据库
var db = server.use({ 
	name: 'weather', 
	username: 'root', 
	password: 'admin' });


exports.orientotest = orientotest;
function orientotest(callback) {
	var aa = null;
	db.query('select * from TODAYWEATHER').then(function (results) {
		console.log("----------zheshi--sss---------------", results);
		//callback(JSON.stringify(results));
	});
	console.log("''''''''''''''''''results'''''111'''''''''''''", aa);
	return aa;}


var ws = require('../service/weaService');
var time = ws.getCurrentTime();
exports.saveWeatherForcast = saveWeatherForcast;
function saveWeatherForcast(list) {
	/*console.log('------------saveWeatherForcast--------------', list);*/

	var len = list.length;
	for (var i = 0; i < len; i++) {
		/*console.log('------------len--------------', list[len-1]);*/
		db.insert().into('ForcastWeather').set({ 
			cityName: list[i].cityName, 
			humidity: list[i].humidity, 
			pressure: list[i].pressure, 
			clouds: list[i].clouds.all, 
			temp: list[i].temp, 
			temp_min: list[i].temp_min, 
			temp_max: list[i].temp_max, 
			icon: list[i].icon, 
			description: list[i].description, 
			weather_main: list[i].weather_main, 
			temp_kf: list[i].temp_kf, 
			sea_level: list[i].sea_level, 
			grnd_level: list[i].grnd_level, 
			wind_speed: list[i].wind_speed, 
			wind_deg: list[i].wind_deg, 
			weatherTime: list[i].weatherTime, 
			createTime: time, 
			editTime: time }).
		all().
		then(function (data) {
			console.log('------------success--------------');});}}




exports.updateWeatherForcast = updateWeatherForcast;
function updateWeatherForcast(list) {
	/*console.log('------------saveWeatherForcast--------------', list);*/
	var len = list.length;
	for (var i = 0; i < len; i++) {
		/*console.log('------------len--------------', list[len-1]);*/
		db.update('ForcastWeather').set({ 
			cityName: list[i].cityName, 
			humidity: list[i].humidity, 
			pressure: list[i].pressure, 
			clouds: list[i].clouds.all, 
			temp: list[i].temp, 
			temp_min: list[i].temp_min, 
			temp_max: list[i].temp_max, 
			icon: list[i].icon, 
			description: list[i].description, 
			weather_main: list[i].weather_main, 
			temp_kf: list[i].temp_kf, 
			sea_level: list[i].sea_level, 
			grnd_level: list[i].grnd_level, 
			wind_speed: list[i].wind_speed, 
			wind_deg: list[i].wind_deg, 
			weatherTime: list[i].weatherTime, 
			editTime: time }).
		all().
		then(function (data) {
			console.log('------------未来3到5天天气更新成功--------------');});}}